package com.bianguo.android.baselibrary.manager;

import com.bianguo.android.baselibrary.bean.PathBean;

import java.util.ArrayList;
import java.util.HashMap;

public class RecordPathManager {

    private static final HashMap<String, ArrayList<PathBean>> groupMp = new HashMap<>();

    /**
     *
     * @param groupName 组名，如：“app”
     * @param pathName 路径名，如：“MainActivity”
     * @param clazz 类对象，如：“MainActivity.class”
     */
    public static void joinGroup(String groupName, String pathName, Class<?> clazz) {
        ArrayList<PathBean> pathBeans = groupMp.get(groupName);
        if (pathBeans == null) {
            pathBeans = new ArrayList<>();
            pathBeans.add(new PathBean(pathName, clazz));
            groupMp.put(groupName, pathBeans);
        } else {
            for (PathBean pathBean : pathBeans) {
                if (!pathName.equals(pathBean.getPath())) {
                    pathBeans.add(new PathBean(pathName, clazz));
                    groupMp.put(groupName, pathBeans);
                }
            }
        }
    }

    /**
     * 根据组名和路径名获取类对象，达到跳转的目的
     *
     * @param groupName 组名
     * @param pathName 路径名
     * @return 跳转目的的class类对象
     */
    public static Class<?> getTargetClass(String groupName, String pathName) {
        ArrayList<PathBean> pathBeans = groupMp.get(groupName);
        if (pathBeans == null) {
            return null;
        }
        for (PathBean pathBean : pathBeans) {
            if (pathName.equalsIgnoreCase(pathBean.getPath())) {
                return pathBean.getClazz();
            }
        }
        return null;
    }
}
